package com.lanou.userLogin.cart.service;

import com.lanou.userLogin.book.dao.BookDao;
import com.lanou.userLogin.book.dao.BookDaoImpl;
import com.lanou.userLogin.book.domain.Book;
import com.lanou.userLogin.book.service.BookService;
import com.lanou.userLogin.cart.domin.CartItem;

import java.util.Iterator;
import java.util.List;

public class CartService {

    private BookDao bdao = new BookDaoImpl();

    BookService bs = new BookService();

    public List<CartItem> cartAdd(String bid, List<CartItem> list, int count) {

        Book book = new Book();
        List<Book> bookList = bs.oneBookLoad(bid);
        for (Book book1 : bookList) {
            book = book1;
        }
        CartItem cartItem = new CartItem();
        cartItem.setBook(book);
        cartItem.setCount(count);
        list.add(cartItem);
        return list;
    }

    public List<CartItem> cartDeleteOne(String bid, List<CartItem> list) {
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            CartItem cl = (CartItem) iterator.next();
            System.out.println(cl.getBook().getBid());
            //System.out.println(bid);
            if (cl.getBook().getBid().equals(bid)) {
                System.out.println("1111111");
                iterator.remove();
            } else {
                System.out.println("2222222");
            }
        }
        return list;
    }


    public List<CartItem> cartDeleteAll(List<CartItem> list) {
        list.clear();
        return list;
    }
}
